package com.src;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

public class TestTwoActivity extends Activity {
	int[] imageIds = new int[] { R.drawable.shuangzi, R.drawable.shuangyu,
			R.drawable.chunv, R.drawable.tiancheng, R.drawable.tianxie,
			R.drawable.sheshou, R.drawable.juxie, R.drawable.shuiping,
			R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu,
			R.drawable.mojie };
	private ImageButton leftImageButton = null;
	private ImageButton rightImageButton = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final Gallery gallery = (Gallery) findViewById(R.id.gallery);
		leftImageButton = (ImageButton) findViewById(R.id.imageButtonLeft);
		rightImageButton = (ImageButton) findViewById(R.id.imageButtonRight);
		leftImageButton.setFocusable(false);
		rightImageButton.setFocusable(false);
		BaseAdapter adapter = new BaseAdapter() {
			@Override
			public int getCount() {
				return imageIds.length;
			}

			@Override
			public Object getItem(int position) {
				return position;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				LinearLayout layout = new LinearLayout(TestTwoActivity.this);
				layout.setOrientation(LinearLayout.VERTICAL);
				layout.setGravity(Gravity.CENTER);
				TextView textView = new TextView(TestTwoActivity.this);
				textView.setText("FREE");
				ImageView imageView = new ImageView(TestTwoActivity.this);
				imageView
						.setImageResource(imageIds[position % imageIds.length]);

				imageView.setScaleType(ImageView.ScaleType.FIT_XY);
				imageView.setLayoutParams(new Gallery.LayoutParams(75, 100));
				TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);

				layout.setBackgroundResource(typedArray.getResourceId(
						R.styleable.Gallery_android_galleryItemBackground, 0));
				layout.addView(imageView);
				layout.addView(textView);
				return layout;
			}
		};
		gallery.setAdapter(adapter);
		gallery.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				System.out.println("+=+");
				return false;
			}
		});
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				Toast toast = Toast.makeText(TestTwoActivity.this, new Integer(
						position).toString(), Toast.LENGTH_SHORT);
				toast.show();
			}
		});

		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// switcher.setImageResource(imageIds[position %
				// imageIds.length]);

				Toast toast = Toast.makeText(TestTwoActivity.this, new Integer(
						position).toString(), Toast.LENGTH_SHORT);
				toast.show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		gallery.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				switch (event.getKeyCode()) {
				case KeyEvent.KEYCODE_DPAD_LEFT:
					System.out.println("Left");
					break;
				case KeyEvent.KEYCODE_DPAD_RIGHT:
					System.out.println("Right");
					break;
				}
				return false;
			}
		});
	}

	// @Override
	// public boolean onKey(View v, int keyCode, KeyEvent event) {
	// switch (keyCode) {
	// case 21:
	// System.out.println("21");
	// break;
	// case 22:
	// System.out.println("22");
	// break;
	// }
	// return false;
	// }
}